package com.hrm.system.controller;

import com.hrm.common.controller.BaseController;
import com.hrm.common.entity.PageResult;
import com.hrm.common.entity.Result;
import com.hrm.common.entity.ResultCode;
import com.hrm.domain.system.Role;
import com.hrm.system.service.RoleService;
import com.hrm.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: 林海湾Sea
 * @Date :  2021/11/26 16:22
 * @Decription: 角色接口（控制端）
 */

@CrossOrigin // 解决跨域
@RestController
@RequestMapping("/sys")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    /**
     * 1、保存角色
     */
    @RequestMapping(value = "/role", method = RequestMethod.POST)
    public Result save(@RequestBody Role role) {

        role.setCompanyId(companyId);
        roleService.addRole(role);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 2、修改角色信息
     */
    @RequestMapping(value = "/role/{id}", method = RequestMethod.PUT)
    public Result update(@PathVariable(value = "id") String id, @RequestBody Role role) {
        // 1、设置修改的部门id
        roleService.update(role);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 3、根据id删除角色
     */
    @RequestMapping(value = "/role/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable(value = "id") String id) {
        roleService.deleteRole(id);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 4、根据id查找角色
     */
    @RequestMapping(value = "/role/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable(value = "id") String id) {
        Role role = roleService.findById(id);
        return new Result(ResultCode.SUCCESS, role);
    }

    /**
     * 查询全部角色
     * @return
     */
    @RequestMapping(value = "/role/list", method = RequestMethod.GET)
    public Result findAll() {
        List<Role> roleList = roleService.findAll(companyId);
        return new Result(ResultCode.SUCCESS, roleList);
    }

    /**
     * 查询角色列表（分页）
     * 分页展示
     * @param page
     * @param size
     * @param map
     * @return
     */
    @RequestMapping(value = "/role", method = RequestMethod.GET)
    public Result findAll(int page, int size, @RequestParam Map map) {
        // map.put("companyId", companyId);
        Page<Role> pageRole = roleService.findByPage(companyId, page, size);
        PageResult pageResult = new PageResult(pageRole.getTotalElements(), pageRole.getContent());
        return new Result(ResultCode.SUCCESS, pageResult);
    }

    /**
     * 分配权限
     * @return
     */
    @RequestMapping(value = "/role/assignPrem", method = RequestMethod.PUT)
    public Result save(@RequestBody Map<String, Object> map) {
        // 1、获取被分配的角色的id
        String roleId = (String) map.get("id");
        // 2、获取权限的id列表
        List<String> permIds = (List<String>) map.get("permIds");
        // 3、调用service完成权限分配
        roleService.assignPerms(roleId, permIds);
        return new Result(ResultCode.SUCCESS);
    }
}
